package com.test.daily.leetcode.y2022.m05.day0524.v02;

import java.util.ArrayList;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/5/24 下午 03:55
 * @version: 1.0
 */
public class Solution {
    public static class Node {
        int happy;
        ArrayList<Node> nexts;

        public Node(int happy) {
            this.happy = happy;
            nexts = new ArrayList<>();
        }
    }

    public static int maxHapp1(Node boss) {
        return process1(boss, false);
    }

    public static int process1(Node head, boolean up) {
        if (up) {
            int ans = 0;
            for (Node next : head.nexts) {
                ans += process1(next, false);
            }
            return ans;
        } else {
            int p1 = head.happy;
            for (Node next : head.nexts) {
                p1 += process1(next, true);
            }
            int p2 = 0;
            for (Node next : head.nexts) {
                p2 += process1(next, false);
            }
            return Math.max(p1, p2);
        }
    }

    public static class Info {
        int yes;
        int no;

        Info(int yes, int no) {
            this.yes = yes;
            this.no = no;
        }
    }

    public static int max2(Node head) {
        Info allInfo = process2(head);
        return Math.max(allInfo.no, allInfo.yes);
    }

    public static Info process2(Node head) {
        if (head == null) {
            return new Info(0, 0);
        }
        int no = 0;
        int yes = head.happy;
        for (Node next : head.nexts) {
            Info nextInfo = process2(next);
            no += Math.max(nextInfo.yes, nextInfo.no);
            yes += nextInfo.no;
        }
        return new Info(yes, no);
    }
}
